

from PyQt4.QtCore import QAbstractListModel, Qt, QVariant, QModelIndex, QString
from PyQt4.QtGui import QIcon

try:
    from numpy import ndarray
except ImportError:
    ndarray = None

import datetime

class SCListModel(QAbstractListModel):
    
    def __init__(self,listDict,parent=None):
        QAbstractListModel.__init__(self,parent)
        self.__data = listDict
    
    def rowCount(self, parent=QModelIndex):
        return len(self.__data)

    def getData(self):
        return self.__data

    def setData(self, index, value, role=Qt.EditRole):
        if not index.isValid():
            return False
        if isinstance(value,QVariant):
            return False
        self.__data.valueToIndex(index.row(),value)
        
    def data(self,index, role = Qt.DisplayRole):
        if not index.isValid():
            return QVariant()
        if role == Qt.DisplayRole:
            name = str(self.__data.keyFromIndex(index.row()))
            return QVariant(QString(name))
        elif role == Qt.DecorationRole:
            value = self.__data.valueFromIndex(index.row())
            if isinstance(value,list):
                return QVariant(QIcon(":/Types/Types/list.png"))
            elif isinstance(value,tuple):
                return QVariant(QIcon(":/Types/Types/tuple.png"))                
            elif isinstance(value,dict):
                return QVariant(QIcon(":/Types/Types/dict.png"))
            elif isinstance(value,float):
                return QVariant(QIcon(":/Types/Types/float.png"))
            elif isinstance(value,int):
                return QVariant(QIcon(":/Types/Types/int.png"))
            elif isinstance(value,basestring):
                return QVariant(QIcon(":/Types/Types/string.png"))
            elif isinstance(value,(datetime.date,datetime.datetime,datetime.timedelta)):
                return QVariant(QIcon(":/Types/Types/date.png"))
            elif isinstance(value,ndarray):
                return QVariant(QIcon(":/Types/Types/array.png"))
            else:
                return QVariant(QIcon(":/Types/Types/unknown.png"))
        elif role == Qt.ToolTipRole:
            value = str(self.__data.valueFromIndex(index.row()))
            return QVariant(QString(value))
        return QVariant()

    def flags(self, index):
        if index.isValid():
            return Qt.ItemFlags(QAbstractListModel.flags(self, index)|\
                                Qt.ItemIsEditable)
